home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Technology Seed / Jan. '98 ATS.toast / NavServices1.0b3 / Navigation Services SDK / Examples / Sampler / Sampler ƒ / Common.c next >
Encoding:
C/C++ Source or Header  |  1998-01-12  |  13.2 KB  |  534 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Common.c
  3.  
  4.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. #pragma segment AppSeg
  9.  
  10. #ifndef __EVENTS__
  11. #include <Events.h>
  12. #endif
  13.  
  14. #ifndef __TRAPS__
  15. #include <Traps.h>
  16. #endif
  17.  
  18. #ifndef __ICONS__
  19. #include <Icons.h>
  20. #endif
  21.  
  22. #ifndef __GESTALT__
  23. #include <Gestalt.h>
  24. #endif
  25.  
  26. #ifndef Common_Defs
  27. #include "Common.h"
  28. #endif
  29.  
  30. extern short gInBackground;
  31.  
  32. // routines used for getting file path names:
  33. OSErr PPrepend(StringPtr dst,StringPtr src);
  34. void PAppend(StringPtr dst, StringPtr src);
  35.  
  36. TrapType GetTrapType(short theTrap);
  37. short NumToolboxTraps(void);
  38.  
  39.  
  40. // ********************************************************************************
  41. // *    ConcatPP()
  42. // ********************************************************************************
  43. unsigned char* ConcatPP(unsigned char* a, unsigned char* b)
  44. {
  45.     short i;    
  46.     for (i = 1;i <= b[0];i++)
  47.         a[++a[0]] = b[i];        
  48.     return (a);
  49. }
  50.  
  51.  
  52. // ********************************************************************************
  53. // *    FSSpecsEq()
  54. // ********************************************************************************
  55. Boolean FSSpecsEq(FSSpec* a,FSSpec* b)
  56. {
  57.     if (a->vRefNum != b->vRefNum)
  58.         return (false);
  59.     if (a->parID != b->parID)
  60.         return (false);
  61.     if (IUEqualString(a->name,b->name))
  62.         return (false);
  63.     return (true);
  64. }
  65.     
  66.     
  67. // ********************************************************************************
  68. // *    rnd()
  69. // *    Returns a 16-bit random number no large than 'max'.
  70. // ********************************************************************************
  71. static SInt16 rnd(SInt16 max)
  72. {
  73.     return ((UInt32) max * (UInt16) Random()) >> 16;
  74. }
  75.  
  76.  
  77. // ********************************************************************************
  78. // *    RandTween()
  79. // *    Returns an 8-bit number between low and high.
  80. // ********************************************************************************
  81. short RandTween(short low, short high)
  82. {
  83.     short result;
  84.     
  85.     result = Random() % (high + 1 - low);
  86.     if (result < 0)
  87.         result = -result;
  88.     result += low;
  89.     return(result);
  90. }
  91.     
  92.  
  93. // ********************************************************************************
  94. // *    GetItemStr()
  95. // ********************************************************************************
  96. unsigned char* GetItemStr(DialogPtr theDialog, short theItem, unsigned char* theString)
  97. {
  98.     Handle ahandle;
  99.     Rect arect;
  100.     short atype;
  101.     
  102.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  103.     GetDialogItemText(ahandle,theString);
  104.     return (theString);
  105. }
  106.  
  107.  
  108. // ********************************************************************************
  109. // *    PokeItemStr()
  110. // ********************************************************************************
  111. void PokeItemStr(DialogPtr theDialog, short theItem, unsigned char *theString)
  112. {
  113.     Handle     ahandle;
  114.     Rect     arect;
  115.     short     atype;
  116.     
  117.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  118.     SetDialogItemText(ahandle,theString);
  119. }
  120.  
  121.  
  122. // ********************************************************************************
  123. // *    PokeCtlVal()
  124. // ********************************************************************************
  125. void PokeCtlVal(DialogPtr theDialog, short theItem, short value)
  126. {
  127.     Handle     ahandle;
  128.     Rect     arect;
  129.     short    atype;
  130.     
  131.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  132.     SetControlValue((ControlHandle)ahandle,value);
  133. }
  134.  
  135.  
  136. // ********************************************************************************
  137. // *    PokeCtlHilite()
  138. // ********************************************************************************
  139. void PokeCtlHilite(DialogPtr theDialog, short theItem, short value)
  140. {
  141.     Handle     ahandle;
  142.     Rect     arect;
  143.     short     atype;
  144.     
  145.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  146.     HiliteControl((ControlHandle)ahandle,value);
  147. }
  148.  
  149.  
  150. // ********************************************************************************
  151. // *    MyP2CCopy()
  152. // ********************************************************************************
  153. char* MyP2CCopy(unsigned char* psrc, char* ctarget)
  154. {
  155.     short i;
  156.     for(i = 0;i < psrc[0];i++)
  157.         ctarget[i] = psrc[i + 1];    
  158.     ctarget[i] = 0;
  159.     return (ctarget);
  160. }
  161.  
  162.  
  163. // ********************************************************************************
  164. // *    MyC2PStr()
  165. // ********************************************************************************
  166. unsigned char* MyC2PStr(char* theStr)
  167. {
  168.     short i,length;
  169.  
  170.     for(i = 0;theStr[i];i++)
  171.         ;
  172.     length = i;
  173.     while(i)
  174.         {
  175.         theStr[i] = theStr[i - 1];
  176.         i--;
  177.         }
  178.     ((unsigned char*)theStr)[0] = length;
  179.     return ((unsigned char *)theStr);
  180. }
  181.  
  182.  
  183. // ********************************************************************************
  184. // *    MyP2CStr()
  185. // ********************************************************************************
  186. char* MyP2CStr(unsigned char* theStr)
  187. {
  188.     short i,length;
  189.     length = theStr[0];
  190.     for(i = 0;i < length;i++)
  191.         theStr[i] = theStr[i + 1];
  192.     theStr[length] = 0;
  193.     return ((char *)theStr);
  194. }
  195.  
  196.  
  197. // ********************************************************************************
  198. // *    DebugCStr()
  199. // ********************************************************************************
  200. void DebugCStr(char* msg)
  201. {
  202.     MyC2PStr(msg);
  203.     DebugStr((unsigned char*)msg);
  204.     MyP2CStr((unsigned char*)msg);
  205. }
  206.  
  207.  
  208. // ********************************************************************************
  209. // *    Str2OSType()
  210. // *    C strings only.
  211. // ********************************************************************************
  212. OSType Str2OSType(Str255 theStr)
  213. {
  214.     OSType num = 0;
  215.     num =  theStr[3] + (theStr[2] << 8) + (theStr[1] << 16) + (theStr[0] << 24);
  216.     return num;    
  217. }
  218.  
  219. // ********************************************************************************
  220. // *    MyStrLen()
  221. // *    C strings only.
  222. // ********************************************************************************
  223. long MyStrLen(char* s)
  224. {
  225.     long count=0;
  226.     while (*s++ != '\0') count++;
  227.     return count;
  228. }
  229.  
  230.  
  231. // ********************************************************************************
  232. // *    myStringToLong()
  233. // *    C strings only.
  234. // ********************************************************************************
  235. long myStringToLong(char* s)
  236. {
  237.     long n = 0;
  238.     short i;
  239.     
  240.     for (i=0;s[i]>='0' && s[i]<='9';i++)
  241.         n = 10*n + (s[i]-'0');
  242.     return n;
  243. }
  244.  
  245.  
  246. // ********************************************************************************
  247. // *    myStringToShort()
  248. // *    C strings only.
  249. // ********************************************************************************
  250. short myStringToShort(char* s)
  251. {
  252.     short n = 0;
  253.     short i;
  254.     
  255.     for(i=0;s[i]>='0' && s[i]<='9';i++)
  256.         n = 10*n + (s[i]-'0');
  257.     return n;
  258. }
  259.  
  260.  
  261. // ********************************************************************************
  262. // *    myStrCpy()
  263. // *    C strings only.
  264. // ********************************************************************************
  265. void myStrCpy(char* dst, char* src)
  266. {
  267.     while(*src != '\0')
  268.         *dst++ = *src++;
  269.     *dst = '\0';
  270. }
  271.  
  272.  
  273. // ********************************************************************************
  274. // *    myStrCat()
  275. // ********************************************************************************
  276. void myStrCat(char* dst, char* src)
  277. {
  278.     dst = dst + MyStrLen(dst);
  279.     myStrCpy(dst,src);
  280. }
  281.  
  282.  
  283. // ********************************************************************************
  284. // *    flag routines for KeyDown events..
  285. // ********************************************************************************
  286. Boolean OptionDown()    { return ModifierDown(optionKey); }
  287. Boolean ShiftDown()     { return ModifierDown(shiftKey); }
  288. Boolean CommandDown()    { return ModifierDown(cmdKey); }
  289. Boolean ControlDown()    { return ModifierDown(controlKey); }
  290.  
  291.  
  292. // *****************************************************************************
  293. // *
  294. // *    ModifierDown()
  295. // *
  296. // *****************************************************************************
  297. Boolean ModifierDown(short modifier)
  298. {
  299.     EventRecord    event;
  300.     (void) OSEventAvail(0,&event);
  301.     return ((event.modifiers & modifier) != 0);
  302. }
  303.  
  304.  
  305. // *****************************************************************************
  306. // *
  307. // *    hiliteTheButton()
  308. // *
  309. // *****************************************************************************
  310. void hiliteTheButton(DialogPtr theDialog, short whichItem)
  311. {
  312.     short        itemType = 0;
  313.     Handle        itemHdl;
  314.     Rect        itemRect;
  315.     long int     startTick;
  316.  
  317.     GetDialogItem(theDialog,whichItem,&itemType,&itemHdl,&itemRect);
  318.     HiliteControl((ControlHandle)itemHdl,1);
  319.     for (startTick=TickCount();TickCount()-startTick<kDelayTick;)
  320.         {} // do nothing...
  321.     HiliteControl((ControlHandle)itemHdl,kActive);
  322. }
  323.  
  324.  
  325. // *****************************************************************************
  326. // *
  327. // *    AdornButton()
  328. // *    
  329. // *****************************************************************************
  330. void AdornButton(DialogPtr theDialog, short whichItem)
  331. {
  332.     GrafPtr    oldPort;
  333.     Handle    itemH;
  334.     short    itemType;
  335.     Rect    itemRect;    
  336.     long     version = 0;
  337.     OSErr    theErr = noErr;
  338.  
  339.     theErr = Gestalt(gestaltSystemVersion,&version);
  340.     if (version <= 0x0760)        // this look is only for Systems after 7.6.0
  341.         {
  342.         GetPort(&oldPort);
  343.         
  344.         SetPort(theDialog);
  345.         GetDialogItem(theDialog,whichItem,&itemType,&itemH,&itemRect);
  346.         PenSize(3,3);
  347.         InsetRect(&itemRect,-4,-4);
  348.         FrameRoundRect(&itemRect,14,14);
  349.         
  350.         PenNormal();
  351.         SetPort(oldPort);
  352.         }
  353. }
  354.  
  355.  
  356. // *****************************************************************************
  357. // *
  358. // *    DrawIconSuite()
  359. // *
  360. // *    This routine draws the appropriate icon into 'destRect' based on the
  361. // *    'resID' of the icon suite.  The caller must set its port before calling
  362. // *    this routine.
  363. // *    
  364. // *****************************************************************************
  365. void DrawIconSuite(short resID, Rect destRect)
  366.     IconSelectorValue    iconType;
  367.     IconAlignmentType    align;
  368.     IconTransformType    transform;
  369.     OSErr                theErr = noErr;
  370.     Handle                iconSuiteHdl;
  371.     
  372.     iconType = svAllAvailableData;
  373.     theErr = GetIconSuite(&iconSuiteHdl,resID,iconType);
  374.     if ((theErr == noErr) && (iconSuiteHdl != nil))
  375.         {
  376.         align = atAbsoluteCenter;
  377.         transform = ttNone;
  378.         theErr = PlotIconSuite(&destRect,align,transform,iconSuiteHdl);
  379.         }
  380. }
  381.  
  382.  
  383. // ********************************************************************************
  384. // *
  385. // *    PAppend()
  386. // *
  387. // ********************************************************************************    
  388. void PAppend(StringPtr dst, StringPtr src)
  389. {
  390.     BlockMove(src + 1, dst + *dst + 1, *src);
  391.     *dst += *src;
  392. }
  393.  
  394.  
  395.  
  396. // ********************************************************************************
  397. // *
  398. // *    PPrepend()
  399. // *        or "Path Append": add 'src' to the end of 'dst'.
  400. // *    - Pascal strings only.
  401. // *
  402. // ********************************************************************************    
  403. OSErr PPrepend(StringPtr dst,StringPtr src)
  404. {
  405.     if(*dst + *src > 255)
  406.         return(-1);
  407.     
  408.     BlockMove(dst + 1, dst + *src + 1, *dst);
  409.     BlockMove(src + 1, dst + 1, *src);
  410.     *dst += *src;
  411.     
  412.     return(0);
  413. }
  414.  
  415.  
  416. // ********************************************************************************
  417. // *
  418. // *    PathNameFromDirID()
  419. // *
  420. // *    Creates a full or partial path name of a given file.
  421. // *    - Inputs 'dirID' and 'vRefNum' and outputs 'fullPathName'.
  422. // *    - fullPathLength = num characters requested.
  423. // *    - full = flag to make full path, rather than just the item name.
  424. // *
  425. // ********************************************************************************    
  426. OSErr PathNameFromDirID(long dirID,
  427.                         short vRefNum,
  428.                         StringPtr fullPathName,
  429.                         short maxPathLength,
  430.                         short full)
  431. {
  432.     DirInfo    block;
  433.     Str255    directoryName;
  434.     OSErr     theErr;
  435.  
  436.     fullPathName[0] = 0;
  437.  
  438.     block.ioDrParID = dirID;
  439.     block.ioNamePtr = directoryName;
  440.     do {
  441.         block.ioVRefNum = vRefNum;
  442.         block.ioFDirIndex = -1;
  443.         block.ioDrDirID = block.ioDrParID;
  444.         theErr = PBGetCatInfoSync((CInfoPBPtr)&block);
  445.         if (theErr)
  446.             return(theErr);
  447.         
  448.         PAppend(directoryName,(StringPtr)"\p:");
  449.         
  450.         if (fullPathName[0] + directoryName[0] >= maxPathLength)
  451.             return(-1);    // actual path name too long for the requested path name size
  452.             
  453.         PPrepend(fullPathName,directoryName);
  454.         }
  455.     while ((block.ioDrDirID != 2) && (full));
  456.         
  457.     return(0);
  458. }
  459.  
  460.  
  461. // *****************************************************************************
  462. // *
  463. // *    AdjustCursor()
  464. // *
  465. // *****************************************************************************
  466. void AdjustCursor(Point theLoc, RgnHandle theRgn)
  467. {    
  468.     WindowPtr    theWindow;
  469.     Document*    theDocument;
  470.     RgnHandle    arrowRgn, iBeamRgn, hiliteRgn;
  471.     Rect        theRect;
  472.     Point        thePoint;
  473.  
  474.     if (gInBackground)
  475.         return;
  476.  
  477.     arrowRgn = NewRgn();
  478.     SetRectRgn(arrowRgn,-32767,-32767,32767,32767);
  479.  
  480.     if ((theWindow = FrontWindow()) &&
  481.         (theDocument = IsDocumentWindow(theWindow)) &&
  482.         (theDocument->theTE != NULL))
  483.         {
  484.         SetPort(theWindow);
  485.  
  486.         iBeamRgn = NewRgn();
  487.         hiliteRgn = NewRgn();
  488.  
  489.         theRect = (**(theDocument->theTE)).viewRect;
  490.         LocalToGlobal((Point*)&(theRect.top));
  491.         LocalToGlobal((Point*)&(theRect.bottom));
  492.         RectRgn(iBeamRgn,&theRect);
  493.  
  494.         CopyRgn(theDocument->hiliteRgn,hiliteRgn);
  495.         thePoint.h = thePoint.v = 0;
  496.         LocalToGlobal(&thePoint);
  497.         OffsetRgn(hiliteRgn,thePoint.h,thePoint.v);
  498.  
  499.         DiffRgn(arrowRgn,hiliteRgn,arrowRgn);
  500.         DiffRgn(arrowRgn,iBeamRgn,arrowRgn);
  501.  
  502.         DiffRgn(iBeamRgn,hiliteRgn,iBeamRgn);
  503.  
  504.         if (PtInRgn(theLoc,iBeamRgn))
  505.             {
  506.             SetCursor(*GetCursor(iBeamCursor));
  507.             CopyRgn(iBeamRgn,theRgn);
  508.             }
  509.         else
  510.             {
  511.             if (PtInRgn(theLoc,hiliteRgn))
  512.                 {
  513.                 SetCursor(&qd.arrow);
  514.                 CopyRgn(hiliteRgn,theRgn);
  515.                 }
  516.             else
  517.                 {
  518.                 SetCursor(&qd.arrow);
  519.                 CopyRgn(arrowRgn,theRgn);
  520.                 }
  521.             }
  522.         
  523.         DisposeRgn(iBeamRgn);
  524.         DisposeRgn(hiliteRgn);
  525.         }
  526.     else
  527.         {
  528.         SetCursor(&qd.arrow);
  529.         CopyRgn(arrowRgn,theRgn);
  530.         }
  531.  
  532.     DisposeRgn(arrowRgn);
  533. }